import java.util.Hashtable;


public class HashCollection extends Hashtable<Integer, SnapShotStack>  {

	private static final int HASHTABLELENGTH = 10001;
	private int[] counter;
	private int numOfElements = 0;
	
	public HashCollection() {
		super(HASHTABLELENGTH);
		counter = new int[HASHTABLELENGTH];
		
	}
	
	public int getNumOfElements() {
		return numOfElements;
	}
	
	public void resetNumOfElementsCounter() {
		numOfElements = 0;
	}
	
	public void insert(BoardState boardState) {
		//boardstate.getHash()
		int hashValue = boardState.getHashIdentifier() % HASHTABLELENGTH;

		
		SnapShotStack snapShotStack = get(hashValue);
		
		if (snapShotStack == null) {
			put(hashValue, new SnapShotStack());
		}
		
		snapShotStack = get(hashValue);
		
		counter[hashValue]++;

//		snapShotStack.addSnapshot(boardState);
		
		
		if (snapShotStack.isStackFull()) {
			SnapShotStack newSnapShotStack = new SnapShotStack();
			newSnapShotStack.setNextSnapShotStack(snapShotStack);
			put(hashValue, newSnapShotStack);	
			newSnapShotStack.addSnapshotNaive(boardState);
		} else {
			snapShotStack.addSnapshotNaive(boardState);
		}
		numOfElements++;
	}
	
	public BoardState retrieveState(BoardState boardState) {
		int hashValue = boardState.getHashIdentifier() % HASHTABLELENGTH;

		SnapShotStack snapShotStack = get(hashValue);
		
		if (snapShotStack == null) {
			return null;
		}
		
		return snapShotStack.retrieveStateInCollectionFast(boardState);
	}
	
	public void print() {
		/*
		int cc = 0;
		for (int i=0 ; i<HASHTABLELENGTH; i++) {
			if (counter[i]!=0) {
				cc++;
				System.out.println(counter[i]);
			}
		}
		System.out.println("count: " + cc);*/
	}
}
